﻿using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Brc_PaaS.Core.Redis
{
    public interface IRedisManager: IDisposable
    {
       
        #region String
        #region 同步方法

        /// <summary>
        /// 保存单个key value
        /// </summary>
        bool StringSet(string key, string value, TimeSpan? expiry = default(TimeSpan?));

        /// <summary>
        /// 保存多个key value
        /// </summary>
        bool StringSet(List<KeyValuePair<RedisKey, RedisValue>> keyValues);

        /// <summary>
        /// 保存一个对象
        /// </summary>
        bool StringSet<T>(string key, T obj, TimeSpan? expiry = default(TimeSpan?));

        /// <summary>
        /// 保存一个对象
        /// </summary>
        bool StringSet<T>(string key, T obj, DateTime expiry);

        /// <summary>
        /// 获取单个key的值
        /// </summary>
        string StringGet(string key);

        /// <summary>
        /// 获取多个Key
        /// </summary>
        RedisValue[] StringGet(List<string> listKey);

        /// <summary>
        /// 获取一个key的对象
        /// </summary>
        T StringGet<T>(string key) where T : class;

        /// <summary>
        /// 为数字增长val
        /// </summary>
        double StringIncrement(string key, double val = 1);

        /// <summary>
        /// 为数字减少val
        /// </summary>
        double StringDecrement(string key, double val = 1);

        #endregion 同步方法

        #endregion String

        #region Hash

       

        /// <summary>
        /// 判断某个数据是否已经被缓存
        /// </summary>
        bool HashExists(string key, string dataKey);

        /// <summary>
        /// 存储数据到hash表
        /// </summary>
        bool HashSet<T>(string key, string dataKey, T t, TimeSpan? expire = null);

        /// <summary>
        /// 存储数据到hash表
        /// </summary>
        void HashMultiSet(string key, Dictionary<string, string> values, TimeSpan? expire = null);

        /// <summary>
        /// 移除hash中的某值
        /// </summary>
        bool HashDelete(string key, string dataKey);

        /// <summary>
        /// 移除hash中的多个值
        /// </summary>
        long HashDelete(string key, List<RedisValue> dataKeys);

        /// <summary>
        /// 从Hash中获取指定key and datakey的值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">不能为空</param>
        /// <param name="dataKey">就是Field</param>
        /// <returns></returns>
        T HashGet<T>(string key, string dataKey) where T : class;

        /// <summary>
        /// 获取hash中的某字段字（以字符串的形式）
        /// </summary>
        /// <param name="key"></param>
        /// <param name="dataKey"></param>
        /// <returns></returns>
        string HashGet(string key, string dataKey);
        /// <summary>
        /// 从hash表获取数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">不能为空</param>
        /// <returns></returns>
        T HashGetAll<T>(string key) where T : class;

        /// <summary>
        /// 以字典的形式获取整个hash对象
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        Dictionary<string,string> HashGetAll(string key);
        /// <summary>
        /// 为数字增长val
        /// </summary>
        double HashIncrement(string key, string dataKey, double val = 1);

        /// <summary>
        /// 为数字减少val
        /// </summary>
        double HashDecrement(string key, string dataKey, double val = 1);

        /// <summary>
        /// 获取hashkey所有Redis key
        /// </summary>
        List<T> HashKeys<T>(string key) where T : class;


        #endregion Hash

        #region List

       

        /// <summary>
        /// 移除指定ListId的内部List的值
        /// </summary>
        void ListRemove<T>(string key, T value) where T : class;

        /// <summary>
        /// 获取指定key的List
        /// </summary>
        List<T> ListRange<T>(string key) where T : class;

        /// <summary>
        /// 入队
        /// </summary>
        void ListRightPush<T>(string key, T value);


        /// <summary>
        /// 出队
        /// </summary>
        T ListRightPop<T>(string key) where T : class;


        /// <summary>
        /// 入栈
        /// </summary>
        void ListLeftPush<T>(string key, T value);


        /// <summary>
        /// 出栈
        /// </summary>
        T ListLeftPop<T>(string key) where T : class;


        /// <summary>
        /// 获取集合中的数量
        /// </summary>
        long ListLength(string key);

       

        #endregion List

        #region SortedSet 有序集合

       
        /// <summary>
        /// 添加
        /// </summary>
        bool SortedSetAdd<T>(string key, T value, double score);


        /// <summary>
        /// 删除
        /// </summary>
        bool SortedSetRemove<T>(string key, T value);


        /// <summary>
        /// 获取全部
        /// </summary>
        //List<T> SortedSetRangeByRank<T>(string key) where T : class;
        List<T> SortedSetRangeByRank<T>(string key, int page = 0, int limit = 10, bool desc = false) where T : class;

        /// <summary>
        /// 获取集合中的数量
        /// </summary>
        long SortedSetLength(string key);



        #endregion SortedSet 有序集合

        #region key

        /// <summary>
        /// 删除单个key
        /// </summary>
        bool KeyDelete(string key);


        /// <summary>
        /// 删除多个key
        /// </summary>
        long KeyDelete(List<string> keys);


        /// <summary>
        /// 判断key是否存储
        /// </summary>
        bool KeyExists(string key);


        /// <summary>
        /// 重新命名key
        /// </summary>
        bool KeyRename(string key, string newKey);


        /// <summary>
        /// 设置Key的时间
        /// </summary>
        bool KeyExpire(string key, TimeSpan? expiry = default(TimeSpan?));

        /// <summary>
        /// 模糊匹配指定的key集合
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        List<string> Keys(string key);

        /// <summary>
        /// 模糊匹配指定的key集合
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        Task<List<string>> KeysAsync(string key);

        List<string> KeysScan(string key);

        Task<List<string>> KeysScanAsync(string key);

        #endregion key

        #region 发布订阅

        /// <summary>
        /// Redis发布订阅  订阅
        /// </summary>
        void Subscribe(string subChannel, Action<RedisChannel, RedisValue> handler = null);


        /// <summary>
        /// Redis发布订阅  发布
        /// </summary>
        long Publish<T>(string channel, T msg);


        /// <summary>
        /// Redis发布订阅  取消订阅
        /// </summary>
        void Unsubscribe(string channel);


        /// <summary>
        /// Redis发布订阅  取消全部订阅
        /// </summary>
        void UnsubscribeAll();


        #endregion 发布订阅

        #region 其他
        /// <summary>
        /// 创建redis事务
        /// </summary>
        /// <returns></returns>
        ITransaction CreateTransaction();

        /// <summary>
        /// 取得数据库;
        /// </summary>
        IDatabase GetDatabase();

        #endregion 其他
    }
}
